package sunwul.mall.commoncore.exception.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import sunwul.mall.commonbase.constant.BusinessStatusEnum;
import sunwul.mall.commonbase.model.Result;
import sunwul.mall.commoncore.exception.BusinessException;

/**
 * @author sunwul
 * @date 2024/12/27 16:31
 * @description 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandle {

    @ExceptionHandler(RuntimeException.class)
    public Result<String> runtimeException(RuntimeException e) {
        log.error(e.getMessage());
        return Result.fail(BusinessStatusEnum.SERVER_INNER_ERROR);
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result<String> accessDeniedException(AccessDeniedException e) {
        log.error(e.getMessage());
        throw e;
    }

    @ExceptionHandler(BusinessException.class)
    public Result<String> runtimeException(BusinessException e) {
        log.error(e.getMessage());
        return Result.fail(e.getMessage());
    }
}
